<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['agent:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['agent:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['agent:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['agent:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="userId" v-if="true"/>
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="代理等级" align="center" prop="agentLevel" />

      <el-table-column label="上级代理" align="center" prop="superiorAgentName" />
      <el-table-column prop="registrationDeductionPermission" align="center" label="权限" width="190">
        <template slot-scope="scope">
          <div>
            扣注册量权限：{{scope.row.registrationDeductionPermission == '0' ? '开':'关'}}
            <br>
            扣单权限：{{scope.row.orderDeductionPermission == '0' ? '开':'关'}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="分佣比例" align="center" prop="commissionRatio" />
      <el-table-column label="安卓注册单价" align="center" prop="androidRegistrationPrice" />
      <el-table-column label="iOS注册单价" align="center" prop="iosRegistrationPrice" />
      <el-table-column label="余额" align="center" prop="balance" />
      <el-table-column label="最后登录IP" align="center" prop="loginIp" />
      <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['agent:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['agent:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代理信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" maxlength="11" />
        </el-form-item>
        <el-form-item label="代理等级" prop="agentLevel" v-if="this.NowAgentLevel == '0'">
          <el-radio-group v-model="form.agentLevel">
            <el-radio label="1">一级</el-radio>
            <el-radio label="2">二级</el-radio>
            <el-radio label="3">三级</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="代理等级" prop="agentLevel" v-if="this.NowAgentLevel == '1'">
          <el-radio-group v-model="form.agentLevel">
            <el-radio label="2">二级</el-radio>
            <el-radio label="3">三级</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="代理等级" prop="agentLevel" v-if="this.NowAgentLevel == '2'">
          <el-radio-group v-model="form.agentLevel">
            <el-radio label="3">三级</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="扣注册量权限" prop="registrationDeductionPermission">
          <el-radio-group v-model="form.registrationDeductionPermission">
            <el-radio label="0">开</el-radio>
            <el-radio label="1">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="扣单权限" prop="orderDeductionPermission">
          <el-radio-group v-model="form.orderDeductionPermission">
            <el-radio label="0">开</el-radio>
            <el-radio label="1">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分佣比例" prop="commissionRatio">
          <el-input v-model="form.commissionRatio" placeholder="请输入分佣比例" />
        </el-form-item>
        <el-form-item label="安卓注册单价" prop="androidRegistrationPrice">
          <el-input v-model="form.androidRegistrationPrice" placeholder="请输入安卓注册单价" />
        </el-form-item>
        <el-form-item label="iOS注册单价" prop="iosRegistrationPrice">
          <el-input v-model="form.iosRegistrationPrice" placeholder="请输入iOS注册单价" />
        </el-form-item>
        <el-form-item label="提现二维码" prop="withdrawCode">
          <imageUpload v-model="form.withdrawCode" />
        </el-form-item>
        <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入用户密码" type="password" maxlength="20" show-password/>
        </el-form-item>
        <el-form-item v-if="form.userId == undefined" label="提现密码" prop="withdrawPassword">
          <el-input v-model="form.withdrawPassword" placeholder="请输入提现密码" type="password" maxlength="20" show-password/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUser, getUser, delUser, addUser, updateUser, getUserInfo} from "@/api/agent/user";

export default {
  name: "User",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代理信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      NowAgentLevel: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        userType: undefined,
        email: undefined,
        phone: undefined,
        sex: undefined,
        avatar: undefined,
        agentLevel: undefined,
        superiorAgent: undefined,
        registrationDeductionPermission: undefined,
        orderDeductionPermission: undefined,
        commissionRatio: undefined,
        androidRegistrationPrice: undefined,
        iosRegistrationPrice: undefined,
        withdrawCode: undefined,
        balance: undefined,
        password: undefined,
        withdrawPassword: undefined,
        isAgent: undefined,
        inviteCode: undefined,
        status: undefined,
        loginIp: undefined,
        loginDate: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        agentLevel: [
          { required: true, message: "代理等级不能为空", trigger: "blur" }
        ],
        registrationDeductionPermission: [
          { required: true, message: "扣注册量权限不能为空", trigger: "blur" }
        ],
        orderDeductionPermission: [
          { required: true, message: "扣单权限不能为空", trigger: "blur" }
        ],
        commissionRatio: [
          { required: true, message: "分佣比例不能为空", trigger: "blur" }
        ],
        androidRegistrationPrice: [
          { required: true, message: "安卓注册单价不能为空", trigger: "blur" }
        ],
        iosRegistrationPrice: [
          { required: true, message: "iOS注册单价不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "登陆密码不能为空", trigger: "blur" }
        ],
        withdrawPassword: [
          { required: true, message: "提现密码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getAgentLevel();
  },
  methods: {
    /** 查询代理信息列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAgentLevel(){
      getUserInfo().then(response => {
        this.NowAgentLevel = response.data.agentLevel;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        userType: undefined,
        email: undefined,
        phone: undefined,
        sex: undefined,
        avatar: undefined,
        agentLevel: undefined,
        superiorAgent: undefined,
        registrationDeductionPermission: undefined,
        orderDeductionPermission: undefined,
        commissionRatio: undefined,
        androidRegistrationPrice: undefined,
        iosRegistrationPrice: undefined,
        withdrawCode: undefined,
        balance: undefined,
        password: undefined,
        withdrawPassword: undefined,
        isAgent: undefined,
        inviteCode: undefined,
        status: undefined,
        delFlag: undefined,
        loginIp: undefined,
        loginDate: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代理信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const userId = row.userId || this.ids
      getUser(userId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改代理信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除代理信息编号为"' + userIds + '"的数据项？').then(() => {
        this.loading = true;
        return delUser(userIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('agent/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
